package com.cunzhang.server.controller.admin;

import com.cunzhang.common.result.PageResult;
import com.cunzhang.common.result.Result;
import com.cunzhang.pojo.dto.EmployeeDTO;
import com.cunzhang.pojo.dto.EmployeeLoginDTO;
import com.cunzhang.pojo.dto.PasswordDTO;
import com.cunzhang.pojo.entity.Employee;
import com.cunzhang.pojo.vo.EmployeeLoginVO;
import com.cunzhang.server.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录接口
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        log.info("员工登录：{}", employeeLoginDTO);
        EmployeeLoginVO employeeLoginVO = employeeService.login(employeeLoginDTO);
        return Result.success(employeeLoginVO);
    }

    /**
     * 员工分页查询接口
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> pageQueryEmployee(String name, Integer page, Integer pageSize){
        log.info("员工分页查询：{}",name);
        PageResult result = employeeService.pageQueryEmployee(name, page, pageSize);

        return Result.success(result);
    }

    /**
     * 新增员工接口
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result addEmployee(@RequestBody EmployeeDTO employeeDTO){
        employeeService.addEmployee(employeeDTO);

        return Result.success();
    }

    /**
     * 启用禁用员工账号
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用账号")
    public Result changeAccountStatus(@PathVariable("status") Integer status, Long id){
        employeeService.changeAccountStatus(status, id);

        return Result.success();
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    public Result<Employee> queryEmployeeById(@PathVariable("id") Long id){
        Employee employee = employeeService.queryEmployeeById(id);
        return Result.success(employee);
    }

    /**
     * 编辑员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result editEmployeeInfo(@RequestBody EmployeeDTO employeeDTO){
        employeeService.editEmployeeInfo(employeeDTO);

        return Result.success();
    }

    /**
     * 退出登录接口
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result logout(){

        return Result.success();
    }

    /**
     * 修改密码
     * @param passwordDTO
     * @return
     */
    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public Result editPassword(@RequestBody PasswordDTO passwordDTO){
        employeeService.editPassword(passwordDTO);

        return Result.success();
    }

}
